<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Docs class
 * 
 * Docs library of utilities and helpers
 * 
 * @package CI Docs
 * @version 0.1b
 * @author chadwithuhc
 * @link http://chadwithuhc.com
 * @copyright 2009 chadwithuhc
 * 
 */
class Docs {

	var $ci;
	
	/**
	 * $var_items
	 * 
	 * Holds all the vars used in the templating
	 * 
	 * @var array
	 * @access public
	 */
	var $var_items;
	
	/**
	 * $config_items
	 * 
	 * Holds all the config options for CI Docs
	 * 
	 * @var array
	 * @access public
	 */
	var $config_items;

	/**
	 * Docs ()
	 * 
	 * Default constructor. Gets CI instance.
	 * 
	 * @access public
	 * @return void
	 */
	function Docs() {
		$this->ci =& get_instance();
		
		// Additional defines used around the site. No need to change these.
		define('ci_docs', 'CI Docs');
		define('ci_docs_version', '0.2b');
		define('ci_docs_date', 1276650650);
		define('ci_docs_homepage', 'http://code.google.com/p/cidocs/');
		define('ci_docs_download_page', 'http://code.google.com/p/cidocs/downloads/list');
		define('blank', '');
		define('ds', '/');
		define('sp', ' ');
		define('nl', "\n");
		define('tb', "\t");
		define('cr', '&copy;');
		define('nbsp', '&nbsp;');
		define('ptsp', ' : ');
		define('bcsp', ' &nbsp;&#8250;&nbsp; ');
		define('ftsp', ' &nbsp;&middot;&nbsp; ');
		define('und', 'underscore');
		define('index', ($this->ci->config->item('index_page') == blank) ? blank : ds . $this->ci->config->item('index_page'));
		define('indexds', (index == blank) ? blank : trim(index, ds) . ds);
		define('suffix', $this->ci->config->item('url_suffix'));
		define('ci_logo','<img src="/app/themes/default/images/ui/ci_logo_flame.jpg" width="150" height="164" border="0" alt="CodeIgniter Logo" />');
	}
	
	/**
	 * get_all_vars (  )
	 * 
	 * Gets all the vars instead of accessing one by one
	 * 
	 * @access public
	 * @return array
	 */
	function get_all_vars() {
		return $this->var_items;
	}
	
	/**
	 * vars ( $name, $value = null )
	 * 
	 * Gets or sets the vars property
	 * 
	 * @access public
	 * @param mixed $name
	 * @param mixed $value (default: null)
	 * @return void
	 */
	function vars($name, $value = null) {
		if (!is_null($value)) {
			$this->var_items[$name] = $value;
		}
		if (!isset($this->var_items[$name])) {
			show_error("Item [ $name ] is not set in your guides config file.");
		}
		return $this->var_items[$name];
	}
	
	/**
	 * config ( $name, $value = null )
	 * 
	 * Gets or sets the config property
	 * 
	 * @access public
	 * @param string $name
	 * @param string $value (default: null)
	 * @return void
	 */
	function config($name, $value = null) {
		if (!is_null($value)) {
			$this->config_items[$name] = $value;
		}
		if (!isset($this->config_items[$name])) {
			show_error("Item [ $name ] is not set in /" . APPPATH . "config/ci_docs.php or your guides config file.");
		}
		return $this->config_items[$name];
	}

	/**
	 * lang_prefix ( $item )
	 * 
	 * A shortcut to get a lang file. Uses lang_prefix_id to check whether an item needs processing or not.
	 * 
	 * @todo turn into prefix_check()
	 * 
	 * @access public
	 * @param string $item
	 * @return string
	 */
	function lang_prefix($item) {
		if (substr($item, 0, count($this->config('lang_prefix_id'))) == $this->config('lang_prefix_id')) {
			return lang(substr($item, count($this->config('lang_prefix_id'))));
		}
		return $item;
	}


	/**
	 * color_syntax ( $additional_brushes = null )
	 * 
	 * Adds the code neccessary to color syntax on a page.
	 * 
	 * @access public
	 * @param array $additional_brushes (default: null)     If you want to add more brushes than the defaults
	 * @return void
	 */
	function color_syntax($additional_brushes = null) {
		$this->ci->themes
			->css('/' . APPPATH . 'plugins/syntaxhighlighter/styles/shCore.css')
			->css('/' . APPPATH . 'plugins/syntaxhighlighter/styles/shThemeDefault.css')
			->js('/' . APPPATH . 'plugins/syntaxhighlighter/scripts/shCore.js')
			->js('/' . APPPATH . 'plugins/syntaxhighlighter/scripts/shBrushPhp.js')
			->js('/' . APPPATH . 'plugins/syntaxhighlighter/scripts/shBrushXml.js')
			->js('/' . APPPATH . 'plugins/syntaxhighlighter/scripts/shBrushJScript.js')
			->js('/' . APPPATH . 'plugins/syntaxhighlighter/scripts/shBrushCss.js');
			
		if (!is_null($additional_brushes)) {
			foreach ($additional_bruses as $brush) {
				$this->ci->themes->js('/' . APPPATH . 'plugins/syntaxhighlighter/scripts/shBrush' . $brush . '.js');
			}
		}
		
		$this->ci->themes->js('SyntaxHighlighter.config.clipboardSwf = "/' . APPPATH . 'plugins/syntaxhighlighter/scripts/clipboard.swf";SyntaxHighlighter.all();', TRUE);
	}
	
	
	/**
	 * load_guide (  )
	 * 
	 * Runs through the whole initial process of loading the guide.
	 * 
	 * @access public
	 * @return void
	 */
	function load_guide() {
		// load CI Docs config
		$this->ci->load->config('ci_docs', TRUE);
		$this->config_items = $this->ci->config->item('ci_docs');
		$this->config('ci_docs', $this->config_items);
		
		// load CI Docs language
		if (file_exists(APPPATH . 'language' . ds . $this->ci->config->item('language') . ds . 'ci_docs_lang.php')) {
			$this->ci->load->language('ci_docs', $this->ci->config->item('language'));
		}
		
		// set the guide tmp name
		/*if (!$this->config('multi_guide_support')) {
			$this->config('guide_name_tmp', url_title($this->config('default_user_guide'), und, TRUE));
			//$this->config('guide', url_title($this->config('default_user_guide'), und));
		}
		else {*/
			$guide_name_tmp = ($this->ci->uri->total_segments() >= 1) ? $this->ci->uri->segment(1) : url_title(ci_docs, und);
			$this->config('guide_name_tmp', strtolower($guide_name_tmp));
		/*}*/
		
		// set paths with trailing slashes
		$this->config('user_guides_dir', $this->config('user_guides_path') . ds);
		$this->config('user_guide_root', $this->config('user_guides_dir') . $this->config('guide_name_tmp') . ds);
		$this->config('config_suffix', '_config');
		$this->config('config_path', '../../' . $this->config('user_guide_root') . 'config' . ds);
		$this->config('licenses_path', $this->config('licenses_path') . ds);
		$this->config('toc_menu_path', $this->config('user_guide_root') . 'config' . ds . $this->config('guide_name_tmp') . '_toc' . EXT);
		
		// load the guides config
		$this->ci->load->config($this->config('config_path') . $this->config('guide_name_tmp') . $this->config('config_suffix'), TRUE);
		$this->var_items = $this->ci->config->item($this->config('config_path') . $this->config('guide_name_tmp') . $this->config('config_suffix'));
		
		// load the user guides language
		if (file_exists($this->config('user_guide_root') . 'language' . ds . $this->ci->config->item('language') . ds . $this->config('guide_name_tmp') . '_lang.php')) {
			$this->_load_language($this->config('guide_name_tmp'), $this->ci->config->item('language'));
		}
		if (count($this->vars('language_files')) > 0) {
			foreach ($this->vars('language_files') as $lang_file) {
				if (file_exists($this->config('user_guide_root') . 'language' . ds . $this->ci->config->item('language') . ds . $lang_file . '_lang.php')) {
					$this->_load_language($lang_file, $this->ci->config->item('language'));
				}
			} 
		}
		
		// set the guide info now that the real config is loaded
		foreach ($this->var_items as $k => $v) {
			$this->config($k, $v);
		}
		$this->config('guide_latest_version', $this->var_items['guide_versions'][0]);
		$this->config('guide_uri', url_title($this->vars('guide_name'), und));
		$this->config('guide_home_url', ds . $this->config('guide_uri') . ds);
		$this->config('guide_url', index . $this->config('guide_home_url'));
		$this->config('toc_url', $this->config('guide_url') . $this->config('toc_uri') . suffix);
				
		// generate the table of contents
		$this->ci->toc->generate();
		
		// process the additional vars AFTER toc has been created
		$data = $this->var_items;
		$data['guide_title'] = $data['guide_name'] . sp . lang('user_guide') . sp . lang('version_title') . $this->config('guide_version');
		$data['guide_user_guide'] = $data['guide_name'] . sp . lang('user_guide');
		$data['guide_home_url'] = $this->config('guide_home_url');
		$data['guide_url'] = $this->config('guide_url');
		$data['toc_url'] = $this->config('toc_url');
		$data['author_meta'] = (isset($data['author_meta'])) ? $data['author_meta'] : $data['author'] . (($this->config('show_email')) ? sp . $data['author_email'] : blank);
		$data['copyright'] = lang('copyright') . sp . cr . sp . $data['copyright_year'];
		$data['title'] = $this->ci->toc->get_title();
		$data['license_name'] = $data['available_licenses'][$data['license_id']];
		$data['license_description'] = $data['guide_name'] . nl . $data['description'] . nl;
		$data['license_copyright'] = $data['copyright'] . sp . $data['author_real_name'] . (($this->config('show_email')) ? sp . htmlspecialchars($data['author_email']) : blank) . nl;
		$data['license_file'] = $this->config('licenses_path') . strtolower($data['license_id']);
		$this->var_items = $data;
		
		// set the root of the views folder to user guides dir
		$this->ci->themes->set_views_root(trim($this->config('user_guide_root'), ds));
	}
	
	/**
	* _load_language ( $langfile = '', $idiom = '', $return = FALSE )
	* 
	 * Load a language file not in regular lang root
	 *
	 * @access	public
	 * @param	mixed	the name of the language file to be loaded. Can be an array
	 * @param	string	the language (english, etc.)
	 * @return	mixed
	 */
	function _load_language($langfile = '', $idiom = '', $return = FALSE) {
		$langfile = str_replace(EXT, blank, str_replace('_lang.', blank, $langfile)) . '_lang' . EXT;

		if (in_array($langfile, $this->ci->lang->is_loaded, TRUE) AND $langfile != 'ci_docs_lang.php') {
			return;
		}

		if ($idiom == blank) {
			$deft_lang = $this->ci->config->item('language');
			$idiom = ($deft_lang == blank) ? 'english' : $deft_lang;
		}

		if (file_exists($this->config('user_guide_root') . 'language' . ds . $idiom . ds . $langfile)) {
			include($this->config('user_guide_root') . 'language' . ds . $idiom . ds . $langfile);
		}
		else {
			show_error('Could not find requested language file: ' . $this->config('user_guide_root') . 'language' . ds . $langfile);
		}

		if (!isset($lang)) {
			log_message('error', 'Language file contains no data: ' . $this->config('user_guide_root') . 'language' . ds . $idiom . ds . $langfile);
			return;
		}

		if ($return == TRUE) {
			return $lang;
		}

		$this->ci->lang->is_loaded[] = $langfile;
		$this->ci->lang->language = array_merge($this->ci->lang->language, $lang);
		unset($lang);

		log_message('debug', 'Language file loaded: ' . $this->config('user_guide_root') . 'language' . ds . $idiom . ds . $langfile);
		return TRUE;
	}
	
}

/* End of file Docs.php */
/* Location: ./app/libraries/Docs.php */